#ifndef __GKOS_PORTIO_H
#define __GKOS_PORTIO_H

#include "lib/global.h"

/*
* Function: inb
* Return: 端口读出的8位数值
* ----------------------------------------------------
* 从端口port读出一个8位数值并返回，为了加速使用了内联函数和寄存器变量
* 端口限定在0~255
*/
static inline uint8_t inb_C(uint16_t port){
    register uint8_t data;
    //=只写
    asm volatile ("inb %%dx, %%al" : "=a"(data) : "d"(port));
    return data;
}
/*
* Function: inb
* Return: void
* ----------------------------------------------------
* 输入一个端口和一个数据，将数据写入端口中
* 端口限定在0~255
*/
static inline void outb_C(uint16_t port, uint8_t data){
    asm volatile ("outb %%al, %%dx" : : "a"(data), "d"(port));
    return ;
}

/*
* Function: outsw_S
* Return: void
* ----------------------------------------------------
* 将addr处起始的word_cnt个字写入端口port
*/
static inline void outsw_C(uint16_t port, const void* addr, uint32_t word_cnt) {
    // +表示先读入，再输出。S标识esi寄存器。ecx要递减，S要写入.
    asm volatile ("cld");
    asm volatile ("rep outsw" : "+S" (addr), "+c" (word_cnt) : "d" (port));
    return ;
}

/*
* Function: insw_S
* Return: void
* ----------------------------------------------------
* 将从端口port读入的word_cnt个字写入addr
*/
static inline void insw_C(uint16_t port, void* addr, uint32_t word_cnt) {
   // +表示先读入，再输出。D标识edi寄存器。ecx要递减，D要读出.
   asm volatile ("cld");
   asm volatile ("rep insw" : "+D" (addr), "+c" (word_cnt) : "d" (port) : "memory");
   return ;
}
#endif